from application.http.models import CodeModel
from application.jobs import AbolishJob
from sanren.extensions import MyLogger


log = MyLogger('abolish')


def abolish_job(*args, **kwargs):
    # 获取作废码信息
    obj_info = CodeModel.Abolish.find(args[0])

    log.logger.info(msg='主任务-作废码')

    if obj_info is None:
        log.logger.info(msg='Abolish Code: 找不到此作废码单')
        return 700404

    # 作废码单状态
    if obj_info.status not in (0, 1, 4):
        log.logger.info(msg='Abolish Code: 已经成功或取消的作废码单不能再进行作废码操作')
        return 700403

    log.logger.info(msg='Abolish Code: 作废任务开启')
    # 作废码单
    abolish_job = AbolishJob(
        params={
            'id': obj_info.id,
            'batch_sn': obj_info.batch_sn,
        }
    )
    abolish_job.run()


def abolish_restore_job(*args, **kwargs):
    print(args, kwargs)
    # 获取作废码信息
    obj_info = CodeModel.Abolish.find(args[0])

    log.logger.info(msg='Abolish Code: 主任务-作废码')

    if obj_info is None:
        log.logger.info(msg='Abolish Code: 找不到此作废码单')
        return 700404

    # 作废码单状态
    if obj_info.status != 2:
        log.logger.info(msg='Abolish Code: 已经成功的作废码单才能再进行还原作废操作')
        return 700403

    log.logger.info(msg='Abolish Code: 作废还原任务开启')
    # 作废码还原
    abolish_job = AbolishJob(
        params={
            'id': obj_info.id,
            'batch_sn': obj_info.batch_sn,
        }
    )
    abolish_job.restore()
